/* Copyright (C) 2012  	Mathieu Marleix <mathieu.marleix@aerian.fr>
 *						Doris Vaton <doris.vaton@gmail.com>
 * 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.shaipicku.mjpegcams;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * The Display of Log of the application.
 * @author Mathieu Marleix <mathieu.marleix@aerian.fr>
 * @author Doris Vaton <doris.vaton@gmail.com>
 *
 */
public class LogCat extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logcat);
		
		Button about = (Button) findViewById(R.id.about);
		about.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LogCat.this,StartActivity.class);
				startActivity(intent);
				finish();
			}
		});
		readLog();
	}
	/**
	 * Write log in a data file
	 * @param context
	 * @param data
	 */
	public static void writeLog(Context context, String data){
		FileOutputStream fOut = null;
		OutputStreamWriter osw = null;

		try{
			fOut = context.openFileOutput("log.dat",MODE_APPEND);
			osw = new OutputStreamWriter(fOut);
			osw.write(data);
			osw.flush();
		}
		catch (Exception e) {
			Log.i("LOG", "Error 1 in debug log file\n");
		}
		finally {
			try {
				osw.close();
				fOut.close();
			} catch (IOException e) {
				Log.i("LOG", "Error 2 in debug log file\n");
			}
		}
	}
	/**
	 * Display Log on the screen.
	 */
	public void readLog(){
		FileInputStream fIn = null;
		InputStreamReader isr = null;

		char[] inputBuffer = new char[900];
		String data = null;

		try{
			fIn = openFileInput("log.dat");
			isr = new InputStreamReader(fIn);
			isr.read(inputBuffer);
			data = new String(inputBuffer);
			TextView log = (TextView) findViewById(R.id.logcat);
			log.setText(data);
			fIn.close();
			isr.close();
			flushLog(this);
		}
		catch (Exception e) {
		}
	}
	/**
	 * Clear the log when the activity is destroyed
	 * @param context
	 */
	public static void flushLog(Context context){
		FileOutputStream fOut = null;
		OutputStreamWriter osw = null;

		try{
			fOut = context.openFileOutput("log.dat",MODE_PRIVATE);
			osw = new OutputStreamWriter(fOut);
			osw.write("");
			osw.flush();
		}
		catch (Exception e) {
			Log.i("LOG", "Error 1 in debug log file\n");
		}
		finally {
			try {
				osw.close();
				fOut.close();
			} catch (IOException e) {
				Log.i("LOG", "Error 2 in debug log file\n");
			}
		}
	}
	

	// If the drawer is open, pressing the key back will close it.
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			flushLog(this);
			Intent intent = new Intent(LogCat.this,StartActivity.class);
			startActivity(intent);
			finish();
			return true;
		}
		return true;
	}
}